package com.longrise.co.ui.login.fragment;

import android.text.InputType;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.longrise.co.R;
import com.longrise.co.ui.carlife.webview.CommonWebViewActivity;
import com.longrise.co.ui.login.contract.LoginContract;
import com.longrise.co.ui.login.model.ImageValidateCode;
import com.longrise.co.ui.login.model.LoginModel;
import com.longrise.co.ui.login.model.SmsCodeBean;
import com.longrise.co.ui.login.presenter.LoginPresenter;
import com.ly.common.base.BaseFragment;
import com.ly.common.commonutils.StringUtils;
import com.ly.common.commonutils.TimeCountUtil;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * Created by luoyang on 2017-6-22.
 */

public class FastRegisterFragment extends BaseFragment<LoginPresenter, LoginModel> implements LoginContract.RegisterView {

    @Bind(R.id.user)
    EditText user;
    @Bind(R.id.pwd)
    EditText pwd;
    @Bind(R.id.confirm_pwd)
    EditText confirmPwd;
    @Bind(R.id.mobile)
    EditText mobile;
    @Bind(R.id.send_sms)
    TextView sendSms;
    @Bind(R.id.sms_code)
    EditText smsCode;
    @Bind(R.id.agree_register)
    CheckBox agreeRegister;
    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.eye)
    ImageView eye;
    @Bind(R.id.confirm_eye)
    ImageView confirmEye;
    @Bind(R.id.image_code)
    EditText imageCodeEt;
    @Bind(R.id.image_code_iv)
    ImageView imageCodeIv;

    private boolean mIsShowPwd = false;

    private boolean mIsConfirmPwd = false;

    private TimeCountUtil mTimeCount;

    private String url = "https://qcda.96520.com/zhejiang_webmap/zhejiang_appdeal";

    private ImageValidateCode mImageValidateCode;

    @Override
    protected int getLayoutResource() {
        return R.layout.fg_register;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        setText(headTitle, "快速注册");
        change2PwdDisplay(false);
        change2ConfirmPwdDisplay(false);
        mPresenter.imageCodeRequest();
    }


    public static FastRegisterFragment newInstance() {
        return new FastRegisterFragment();
    }

    @OnClick({R.id.send_sms, R.id.agree_register, R.id.submit_register,R.id.eye, R.id.confirm_eye,R.id.register_protocol,R.id.image_code_iv})
    public void onViewClicked(View view) {
        if (!checkDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.send_sms:
                if (mobile != null) {
                    mPresenter.sendSmsRequest(mobile.getText().toString(), "1");
                }

                break;
            case R.id.agree_register:
                break;
            case R.id.submit_register:
                if(mImageValidateCode == null) {
                    showErrorTip("图片验证码获取有误");
                    return ;
                }
                mPresenter.registerRequest(user.getText().toString(), mobile.getText().toString(), smsCode.getText().toString(),
                         pwd.getText().toString(), confirmPwd.getText().toString(), agreeRegister.isChecked(), imageCodeEt.getText().toString(), mImageValidateCode.getImgid());
                break;
            case R.id.eye:
                change2PwdDisplay(!mIsShowPwd);
                break;
            case R.id.confirm_eye:
                change2ConfirmPwdDisplay(!mIsConfirmPwd);
                break;
            case R.id.register_protocol:
//                start(RegisterProtocolFragment.newInstance());
                CommonWebViewActivity.gotoWebActivity(getContext(),"注册协议", url);
                break;
            case R.id.image_code_iv:
                mPresenter.imageCodeRequest();
                break;
        }
    }

    private void change2PwdDisplay(boolean isShowPwd) {
        this.mIsShowPwd = isShowPwd;
        int resourceId = isShowPwd ? R.drawable.eye_orange : R.drawable.eye;
        eye.setBackgroundResource(resourceId);

        pwd.setInputType(isShowPwd ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }

    private void change2ConfirmPwdDisplay(boolean isShowPwd) {
        this.mIsConfirmPwd = isShowPwd;
        int resourceId = isShowPwd ? R.drawable.eye_orange : R.drawable.eye;
        confirmEye.setBackgroundResource(resourceId);

        confirmPwd.setInputType(isShowPwd ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }

    @Override
    public void sendSmsSuccess(SmsCodeBean bean) {
        showLongToast("发送验证码成功");
        sendSms.setEnabled(false);
        if (mTimeCount == null) {
            mTimeCount = new TimeCountUtil(60 * 1000, 1000, sendSms);
        }
        mTimeCount.start();
    }

    @Override
    public void getImageValidateCodeSuccess(ImageValidateCode bean) {
        mImageValidateCode = bean;
        if(imageCodeIv != null && bean != null && !StringUtils.isEmpty(bean.getImg())) {
            Glide.with(getContext())
                    .load(bean.getImg())
                    .crossFade()
                    .into(imageCodeIv);
//            ImageLoaderUtils.display(getContext(), imageCodeIv, bean.getImg());
        }
    }

    @Override
    public void requestSuccess(Object o) {
        _mActivity.onBackPressed();
    }


    @OnClick(R.id.head_back)
    public void onViewClicked() {
        _mActivity.onBackPressed();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mTimeCount != null) {
            mTimeCount = null;
        }
    }

}
